package org.gangcai.mac.shop.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.IntentUtils;
import com.blankj.utilcode.util.SPUtils;
import com.bumptech.glide.Glide;

import org.gangcai.mac.shop.R;
import org.gangcai.mac.shop.bean.MyInviteBean;
import org.gangcai.mac.shop.common.BaseActivity;
import org.gangcai.mac.shop.constants.Constant;
import org.gangcai.mac.shop.manager.RetrofitManager;
import org.gangcai.mac.shop.oberver.CommonObserver;
import org.gangcai.mac.shop.views.TranspanentTitleBar;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.android.schedulers.AndroidSchedulers;

public class MyInviteActivity extends BaseActivity {


    @BindView(R.id.invite_count)
    TextView inviteCount;
    @BindView(R.id.invite_money)
    TextView inviteMoney;
    @BindView(R.id.qurCodeIv)
    ImageView qurCodeIv;
    @BindView(R.id.extension_code)
    TextView extensionCode;
    @BindView(R.id.reward_rate)
    TextView rewardRate;
    @BindView(R.id.getLinkBtn)
    TextView getLinkBtn;
    @BindView(R.id.userListBtn)
    TextView userListBtn;
    @BindView(R.id.beginAgentBtn)
    TextView beginAgentBtn;
    @BindView(R.id.titleBar)
    TranspanentTitleBar titleBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_invite);
        ButterKnife.bind(this);
        titleBar.getBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        loadData();
    }

    private void loadData() {
        prompDialog.showLoading("请等待");
        RetrofitManager.create().my_invitecode(SPUtils.getInstance().getString("uid"))
                .compose(this.<MyInviteBean>bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CommonObserver<MyInviteBean>(prompDialog) {
                    @Override
                    public void onSuccess(MyInviteBean value) {
                        if (value.getCode() == Constant.CODE_SUCC) {
                            inviteCount.setText(value.getInvite_count() + "人");
                            inviteMoney.setText(value.getInvite_money() + "元");
                            extensionCode.setText("我的推广码: " + value.getExtension_code());
                            rewardRate.setText("我的奖励率: " + value.getReward_rate() + "%");
                        }
                    }
                });

        //加载二维码
        Glide.with(this).load(RetrofitManager.server_address + "my_qrcode?uid=3").into(qurCodeIv);


    }

    @OnClick({R.id.getLinkBtn, R.id.userListBtn, R.id.beginAgentBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.getLinkBtn:
                Intent shareTextIntent = IntentUtils.getShareTextIntent("快来使用" + getApplication().getString(R.string.app_name) + "吧");
                startActivity(shareTextIntent);
                break;
            case R.id.userListBtn:
                ActivityUtils.startActivity(this, UserListActivity.class);

                break;
            case R.id.beginAgentBtn:

                //申请成为中介
                ActivityUtils.startActivity(this, AgentActivity.class);
                break;
        }
    }
}
